angular.module('Controller')
.directive('qgyOnlineCheckIn',[()->
    return {
        restrict:'E'
        replace:true
        templateUrl:'templates/business-manage/check-in/online-check-in.html'
        controller:'QgyOnlineCheckInCtrl'
    }
])

.controller 'QgyOnlineCheckInCtrl',($scope,$http,urls,wfAlert,$localStorage)->
    ''
    $scope.person={
        idcardFront:'images/id_card_front.jpg'
        idcardBack:'images/id_card_back.jpg'
        frontUploaded:false
        backUploaded:false
    }
    if $scope.contractData.cotenant==undefined
        $scope.contractData.cotenant=[]
    loadIdCard=()->
        back=$scope.contractData?.idcardBack
        front=$scope.contractData?.idcardFront
        imgReg=/^.*[^a][^b][^c]\.(?:png|jpg|bmp|gif|jpeg)$/
        if front && imgReg.test(front)
            $scope.person.idcardFront=front
            $scope.person.frontUploaded=true
        if back && imgReg.test(back)
            $scope.person.idcardBack=back
            $scope.person.backUploaded=true
    $('#on-idcard-front').change(()->
        formData = new FormData()
        formData.append('file', $('#on-idcard-front')[0].files[0])
        $http({
            url:urls.backend + "/image?token=" + $localStorage['token'],
            method:'POST'
            headers:{
                'Content-Type': undefined
            }
            data:formData
            transformRequest: angular.identity
        }).success((resp)->
            if resp.code==0
                saveUrl = resp.message.saveUrl+""
                if(saveUrl.indexOf("http")=='0')
                    $scope.person.idcardFront = saveUrl
                else
                    $scope.person.idcardFront=urls.backend+'/public/'+saveUrl
        )
    )
    $('#on-idcard-back').change(()->
        formData = new FormData()
        formData.append('file', $('#on-idcard-back')[0].files[0])
        $http({
            url:urls.backend + "/image?token=" + $localStorage['token'],
            method:'POST'
            headers:{
                'Content-Type': undefined
            }
            data:formData
            transformRequest: angular.identity
        }).success((resp)->
            if resp.code==0
                saveUrl = resp.message.saveUrl+""
                if(saveUrl.indexOf("http")=='0')
                    $scope.person.idcardBack = saveUrl
                else
                    $scope.person.idcardBack=urls.backend+'/public/'+saveUrl
        )
    )
    _.extend $scope,
        uploadIDCFront:()->
            $('input[id=on-idcard-front]').click();
            ''
        uploadIDCBack:()->
            $('input[id=on-idcard-back]').click();
            ''
        onlineCheckIn:()->
#            if !$scope.person.backUploaded || !$scope.person.frontUploaded
#                wfAlert.toastError('请上传身份证正反面')
#                return
            reqData={
                idcardFront:$scope.person.idcardFront
                idcardBack:$scope.person.idcardBack
            }
            console.log($scope.contractData)
            $http.post("/contract/checkIn/#{$scope.contractData.id}",reqData)
            .success((resp)->
                if resp.code==0
                    $scope.contractData.checked=true
                    wfAlert.toastInfo('入住成功')
                else
                    wfAlert.toastError(resp.message)
            )
